package com.ctcc.serviceImpl;

import com.ctcc.entity.Department;
import com.ctcc.entity.LogIofn;
import com.ctcc.repository.DeptRepository;
import com.ctcc.repository.LogInfoRepository;
import com.ctcc.util.HttpUtil;
import com.mashape.unirest.http.JsonNode;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Created by Peanut on 2018/1/31.
 */
@Service
public class DepartmentImpl {
    Log log= LogFactory.getLog(DepartmentImpl.class);
    @Autowired    HttpUtil httpUtil;
    @Autowired    DeptRepository deptRepository;
    @Autowired   LogInfoRepository logInfoRepository;
    public  void getAllDept(String url,JsonNode jsonNode) throws Exception{

        if(StringUtils.isNotEmpty(url)&&StringUtils.isNotEmpty(jsonNode.toString())) {

            String jsonStr = httpUtil.org_yunzhijia(url, jsonNode).toString();
            if (StringUtils.isNotEmpty(jsonStr)) {
                log.info("获取接口数据成功");
            }
            JSONObject jsonObject = new JSONObject(jsonStr);
            if ("true".equals(String.valueOf(jsonObject.get("success")))&&"100".equals(String.valueOf(jsonObject.get("errorCode")))) {
                JSONArray jsonDeptArray = jsonObject.getJSONArray("data");
                if(jsonDeptArray.length()>0){
                    log.info("开始删除数据");
                    Date delStartTime = new Date();
                    deptRepository.deleteAll();
                    log.info("删除数据完毕");
                    Date delEndTime = new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
                    String startDate = formatter.format(delStartTime);
                    String endDate = formatter.format(delEndTime);
                    LogIofn logIofnDel = new LogIofn();
                    logIofnDel.setStartDate(startDate);
                    logIofnDel.setEndDate(endDate);
                    logIofnDel.setAction("重新导入数据时，将原来的数据全部清空完毕");
                    logInfoRepository.save(logIofnDel);
                    Date addStartTime = new Date();
                    for (int i = 0; i < jsonDeptArray.length(); i++) {
                        log.info("开始新导入数据，第" + i + "    ");
                        Department dept = new Department();
                        JSONObject jsonObjectDept = (JSONObject) jsonDeptArray.get(i);
                        dept.setId(jsonObjectDept.getString("id"));
                        dept.setName(jsonObjectDept.getString("name"));
                        dept.setDepartment(jsonObjectDept.getString("department"));
                        dept.setParentId(jsonObjectDept.getString("parentId"));
                        dept.setWeights(jsonObjectDept.getInt("weights"));
                        deptRepository.save(dept);
                        log.info("导入第" + i + "数据成功");
                    }
                    Date addEndTime = new Date();
                    String addStartDate = formatter.format(addStartTime);
                    String addEndDate = formatter.format(addEndTime);
                    LogIofn logIofnAdd = new LogIofn();
                    logIofnAdd.setStartDate(addStartDate);
                    logIofnAdd.setEndDate(addEndDate);
                    logIofnAdd.setAction("重新导入数据完毕");
                    logInfoRepository.save(logIofnAdd);
                }
            }
            else {
                log.info("获取数据失败，无法导入数据");
            }
        }

    }

}
